﻿using ClassLibrary1.IService;
using ClassLibrary1.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Senparc.Weixin.MP.Entities.Request;

namespace WebApplication1.Controllers
{
    /// <summary>
    /// 微信网页授权
    /// </summary>
    public class WeChatController : Controller
    {
        /// <summary>
        /// 接口配置信息
        /// </summary>
        /// <param name="postModel"></param>
        /// <param name="echostr"></param>
        /// <returns></returns>
        [ActionName("Index")]
        [AllowAnonymous]
        public IActionResult Index(PostModel postModel, string echostr)
        {
            WeChatService weChatService = new WeChatService();
            var stt = weChatService.GetWeiXinCode(postModel, echostr);
            return Content(stt);
        }


        /// <summary>
        /// 按钮信息  触发回调
        /// </summary>
        /// <param name="ReturnUrl"></param>
        /// <returns></returns>
        [ActionName("Login")]
        [AllowAnonymous]
        public IActionResult Login(string ReturnUrl)
        {
            //进这个方法，ReturnUrl这个字段的值是前端页面地址
            WeChatService weChatService = new WeChatService();
            var stt = weChatService.Login(ReturnUrl);
            return Redirect(stt);
        }

        /// <summary>
        ///回调
        /// </summary>
        /// <param name="code"></param>
        /// <param name="state"></param>
        /// <param name="returnUrl"></param>
        /// <returns></returns>
        [ActionName("BaseCallback")]
        [AllowAnonymous]
        public IActionResult BaseCallback(string code, string state, string returnUrl)
        {
            //returnUrl是前端页面地址，code，state这两个字段，微信授权完跳转到这个方法的时候，这两个值就带着
            WeChatService weChatService = new WeChatService();
            var stt = weChatService.BaseCallback(code, state, returnUrl);
            return Redirect(stt);
        }

    }
}
